#include "MainHeader.h"



int LoadThisDoor(int DoorNo){
	char	cboBuf[512] = {0};
	sprintf(cboBuf,"Room Door: %d \n Actual Door: %X",DoorNo,DoorBuf.DoorNotes[DoorNo]);
	SetWindowText(GetDlgItem(DoorWin,lblDoor),cboBuf);
	Combos[dType].SetListIndex(Doors[DoorBuf.DoorNotes[DoorNo]].DoorType);
	Combos[dConn ].SetListIndex(Doors[DoorBuf.DoorNotes[DoorNo]].DestDoor);
	Combos[dOwner].SetListIndex(Doors[DoorBuf.DoorNotes[DoorNo]].OwnerRoom);
	sprintf(cboBuf,"%X",Doors[DoorBuf.DoorNotes[DoorNo]].ExitDoorLength);
	SetWindowText(GetDlgItem(DoorWin,txtLength),cboBuf);
	nHScroll[sWidth] = (Doors[DoorBuf.DoorNotes[DoorNo]].DWidth - Doors[DoorBuf.DoorNotes[DoorNo]].XEntrance)+1;
	nVScroll[sHeight] = (Doors[DoorBuf.DoorNotes[DoorNo]].DHeight - Doors[DoorBuf.DoorNotes[DoorNo]].YEntrance)+1;
	sprintf(cboBuf,"%d",(Doors[DoorBuf.DoorNotes[DoorNo]].DWidth - Doors[DoorBuf.DoorNotes[DoorNo]].XEntrance)+1);
	SetWindowText(GetDlgItem(DoorWin,txtDWidth),cboBuf);
	sprintf(cboBuf,"%d",(Doors[DoorBuf.DoorNotes[DoorNo]].DHeight - Doors[DoorBuf.DoorNotes[DoorNo]].YEntrance)+1);
	SetWindowText(GetDlgItem(DoorWin,txtDHeight),cboBuf);
	

	CurDoor = DoorNo;
	ConnectDoor(DoorBuf.DoorNotes[DoorNo]);
	if(ConnectExists) {
		ConnControls(1);
	}else{
		ConnControls(0);
	}
	return 0;
}

BOOL CALLBACK DwProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	int i;
	char cboBuf[100];
	
	switch (message)
	{
		
	case WM_INITDIALOG:	// when dialog is first created
		DoorWin = hwnd;
		
		nMaxVScroll[sWidth] = 0xFF;
		nMaxVScroll[sHeight] = 0xFF;
		
		Combos[dType].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboDoorType)));
		Combos[dConn].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboConn)));
		Combos[dOwner].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboOwner)));
		Combos[cConnect].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboConnect)));
		Combos[cA1].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboArea1)));
		Combos[cD1].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboDoor1)));
		Combos[cD2].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboDoor2)));
		Combos[cA2].Init(GetDlgItem(hwnd,(int)MAKEINTRESOURCE(cboArea2)));
		
		
		for(i = 0; i<0x100;i++){
			sprintf(cboBuf,"%X",i);
			Combos[dType].Additem(cboBuf);
			Combos[dConn].Additem(cboBuf);
			Combos[dOwner].Additem(cboBuf);
		}
		Combos[dType].SetListIndex(0);
		Combos[dConn].SetListIndex(0);
		Combos[dOwner].SetListIndex(0);
		ShowWindow(hwnd,SW_HIDE);
		break;
	case BN_CLICKED:
		
		break;
	case WM_LBUTTONDOWN:
		
		break;
	case WM_COMMAND:
		if(LOWORD(wParam) == IDCANCEL) {
			ShowWindow(DoorWin,SW_HIDE);
			//EndDialog(DoorWin,0);
		}
		if(LOWORD(wParam)==cmdSaveDoor){
			SaveThisDoor(CurDoor);
		}
		if(LOWORD(wParam) == cmdSaveConn) SaveConnections();
		if(LOWORD(wParam)==cboConn){
			Doors[DoorBuf.DoorNotes[CurDoor]].DestDoor = (u8)Combos[dConn].GetListIndex();
			
			ConnectDoor(Doors[DoorBuf.DoorNotes[CurDoor]].DestDoor);
			
		}
		if(LOWORD(wParam) == cboConnect){
			SetConnections((u8)Combos[cConnect].GetListIndex());			  
		}
		break;
		
	case WM_VSCROLL:	// exact same idea, but V scroll instead of H scroll
		
		
		
		
	case WM_DESTROY:
		
		/// myBackBuffer.Destroy();
		break;
	}
	
	return 0;
}

int SaveThisDoor(int DoorNo){
	char	cboBuf[512];
	int blah=0;
	
	sprintf(cboBuf,"Room Door: %d \n Actual Door: %d",DoorNo,DoorBuf.DoorNotes[DoorNo]);
	SetWindowText(GetDlgItem(DoorWin,lblDoor),cboBuf);
	Doors[DoorBuf.DoorNotes[DoorNo]].DoorType = (u8)Combos[dType].GetListIndex();
	Doors[DoorBuf.DoorNotes[DoorNo]].DestDoor= (u8)Combos[dConn ].GetListIndex();
	Doors[DoorBuf.DoorNotes[DoorNo]].OwnerRoom=(u8)Combos[dOwner].GetListIndex();
	GetWindowText(GetDlgItem(DoorWin,txtLength),cboBuf,3);
	sscanf(cboBuf,"%X",&blah);
	if(blah > 255) blah = 255;
	if(blah < 0) blah = 0;
	sprintf(cboBuf,"%X",Doors[DoorBuf.DoorNotes[DoorNo]].ExitDoorLength);
	Doors[DoorBuf.DoorNotes[DoorNo]].ExitDoorLength = blah;
	
	
	GetWindowText(GetDlgItem(DoorWin,txtDWidth),cboBuf,4);
	sscanf(cboBuf,"%d",&blah);
    Doors[DoorBuf.DoorNotes[DoorNo]].DWidth = blah+ Doors[DoorBuf.DoorNotes[DoorNo]].XEntrance-1;


	GetWindowText(GetDlgItem(DoorWin,txtDHeight),cboBuf,4);
	sscanf(cboBuf,"%X",&blah);
	Doors[DoorBuf.DoorNotes[DoorNo]].DHeight = blah + Doors[DoorBuf.DoorNotes[DoorNo]].YEntrance-1;
	blah =	Doors[DoorBuf.DoorNotes[DoorNo]].DestDoor;
	Doors[blah].DestDoor = DoorBuf.DoorNotes[DoorNo];
	SaveDoors();
	LoadThisDoor(DoorNo);
	//DisplayDoors();
	//InvalidateRect(hwndMap,0,1);
	DrawRoom();
	return 0;
	
}

int ConnControls(bool How){
	
	if(How){
		Combos[cA1].Enable();
		Combos[cD1].Enable();
		Combos[cdA1].Enable();
		Combos[cA2].Enable();
		Combos[cD2].Enable();
		Combos[cdA2].Enable();
		Combos[cConnect].Enable();
	}else{
		Combos[cA1].Disable();
		Combos[cD1].Disable();
		Combos[cdA1].Disable();
		Combos[cA2].Disable();
		Combos[cD2].Disable();
		Combos[cdA2].Disable();
		Combos[cConnect].Disable();
	}
	return 0;
}

int SaveConnections(){
	int i = 0;
	long offset=0;

	int c = PSCE[Combos[cConnect].GetListIndex()]+1;
	DoorConnections.DoorConnects[c].sA1 =DoorConnections.DoorConnects[c].dA2= Combos[cA1].GetListIndex();
	DoorConnections.DoorConnects[c].D1=Combos[cD1].GetListIndex();
	DoorConnections.DoorConnects[c].dA1=DoorConnections.DoorConnects[c].sA2=Combos[cdA1].GetListIndex();
    DoorConnections.DoorConnects[c].D2=Combos[cD2].GetListIndex();
	GameEngine->OpenFile();

	FILE* fp = GameEngine->oFP();
	char max=0;
	
	
	fseek(fp,GameEngine->GetConnection(),SEEK_SET);
	fread(&offset,4,1,fp);
	fseek(fp,offset-0x8000000,SEEK_SET);
	//for(int i = 0;;i+=3){
	
	
	
	fseek(GBA.ROM,offset - 0x8000000,SEEK_SET);
	for(i = 0;i<DoorConnections.Connects;i++){
		fputc(DoorConnections.DoorConnects[i].sA1, fp);
		fputc(DoorConnections.DoorConnects[i].D1, fp);
		fputc(DoorConnections.DoorConnects[i].dA1,fp);
		fputc(DoorConnections.DoorConnects[i].sA2, fp);
		fputc(DoorConnections.DoorConnects[i].D2, fp);
		fputc(DoorConnections.DoorConnects[i].dA2, fp);
	}
	fclose(fp);
	EnableConnections();
	return 0;
}
